//角色路由映射
import roleConfig from './role-menu-config';   
import base from './base';                   //默认路由
import agent from './agent';                 //运营角色类操作
import facilitator from './facilitator';     //管理角色类操作
import * as _ from 'lodash';

let roleMenuMap = {
    'base': base,
    'agent': agent,
    'facilitator': facilitator
};

//角色归类分类
function roleMenuGroup(role) {
	let roleType = 'base';
    if (role == 'base') {
        roleType = 'base';
    }
    return roleType;
}

//角色菜单配置
function filterRoleMenu(roleMenu,roleMenuConfig) {
    for (var i in roleMenuConfig) {
        let obj = overMenu(i,roleMenu[i],roleMenuConfig[i]);
        if (obj) {
           roleMenu[i] = obj;
        }
    }
    return roleMenu;
}

//菜单属性覆盖
function overMenu(menuName,roleMenuItem,roleMenuConfigItem) {
	//console.log('overMenu',roleMenuItem,roleMenuConfigItem);
	if (roleMenuItem) {
		for (let i in roleMenuConfigItem) {
			if (i == 'children') {
			    for(let j in roleMenuConfigItem[i]) {
			        for (let t in roleMenuConfigItem[i][j]) {
			       	    roleMenuItem[i][j][t] = roleMenuConfigItem[i][j][t];
			        }
			    }
			} else {
			    roleMenuItem[i] = roleMenuConfigItem[i];
		    }
		}
    } else {
        console.log('It\'s not find '+menuName+' menu config on Menu.');
    }
    return roleMenuItem;
}

//MenuObjToArr
function getMenuArr(Menu) {
	let arr = [],
	    obj = Menu;
    //console.log(obj)
	for (let i in obj) {
        //console.log(obj[i]);
		if (obj[i].children) {
            let childrenArr = [];
            for (let j in obj[i].children) {
                childrenArr.push(obj[i].children[j]);
            }
           obj[i].children = childrenArr;
		}
		arr.push(obj[i]);
	}
	return arr;
}

//返回角色路由菜单
function getRoleMenu(role) {
	//console.log('getRoleMenu',role);
	let config = roleConfig[role];
	//console.log('config',config);
    let roleType = roleMenuGroup(role);
    //console.log('roleType',roleType);
    let roleObj = roleMenuMap[roleType];
    //console.log('roterArr',roterArr);
    let MenuObj = filterRoleMenu(roleObj,config);
    //路由对象转数组
    let MenuArr = getMenuArr(MenuObj);
    //console.log('getRoleMenu',MenuArr);
	return MenuArr;
}

//返回角色菜单列表
const MenuMap = {
	getRoleMenu
}

export default MenuMap;